﻿Public Class frmNhapHang

    Private Sub btnThemHang_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThemHang.Click
        Dim frm As frmThemHang = New frmThemHang()
        frm.Tag = dgvDanhSach
        frm.Show()
    End Sub

    Private Sub frmNhapHang_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ThemCot()
    End Sub

    Public Sub ThemCot()
        Dim TenHang, Gia, DonVi, SoLuong, NhaCungCap, XuatXu, LoaiHang, ViTri, HinhAnh As DataGridViewTextBoxColumn
        TenHang = New DataGridViewTextBoxColumn()
        TenHang.HeaderText = "Tên sản phẩm"
        TenHang.Width = 100

        SoLuong = New DataGridViewTextBoxColumn()
        SoLuong.HeaderText = "Số lượng"
        SoLuong.Width = 100

        Gia = New DataGridViewTextBoxColumn()
        Gia.HeaderText = "Giá"
        Gia.Width = 100

        DonVi = New DataGridViewTextBoxColumn()
        DonVi.HeaderText = "Đơn vị"
        DonVi.Width = 100

        NhaCungCap = New DataGridViewTextBoxColumn()
        NhaCungCap.HeaderText = "Nhà cung cấp"
        NhaCungCap.Width = 100

        XuatXu = New DataGridViewTextBoxColumn()
        XuatXu.HeaderText = "Xuất xứ"
        XuatXu.Width = 100

        LoaiHang = New DataGridViewTextBoxColumn()
        LoaiHang.HeaderText = "Loại hàng"
        LoaiHang.Width = 100

        ViTri = New DataGridViewTextBoxColumn()
        ViTri.HeaderText = "Vị trí"
        ViTri.Width = 100

        HinhAnh = New DataGridViewTextBoxColumn()
        HinhAnh.HeaderText = "Hình ảnh"
        HinhAnh.Width = 100

        dgvDanhSach.Columns.AddRange(New DataGridViewColumn() {TenHang, Gia, DonVi, SoLuong, NhaCungCap, XuatXu, LoaiHang, ViTri, HinhAnh})
    End Sub

    Private Sub btnNhapHang_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNhapHang.Click
        Dim th As TapHoa = New TapHoa()
        th.m_dtNgayGio = System.DateTime.Now
        th.m_intIDNhanVien = 1
        th.m_intSoLuong = Integer.Parse(lbSoLuong.Text)
        th.m_intTongTien = Integer.Parse(lbTongTien.Text)
        Dim dt As DataTable = New DataTable()
        dt = TapHoaDAO.LayIDNhapHang(th)
        Dim IDNhapHang As Integer
        IDNhapHang = Integer.Parse(dt.Rows(0)("ID").ToString())

        Dim SoLuongDong As Integer = dgvDanhSach.Rows.Count - 1
        Dim i As Integer = 0
        While (i < SoLuongDong)
            Dim th2 As TapHoa = New TapHoa()
            Dim IDHangHoa As Integer
            Dim th3 As TapHoa = New TapHoa()
            th.m_strTenHangHoa = dgvDanhSach.Rows(i).Cells(0).Value
            Dim dt3 As DataTable = New DataTable()
            dt3 = TapHoaDAO.TimHangHoaTheoTen(th)
            If (dt3.Rows.Count > 0) Then
                Dim dt4 As DataTable = New DataTable()
                th3.m_intSoLuong = Integer.Parse(dgvDanhSach.Rows(i).Cells(3).Value)
                dt4 = TapHoaDAO.CapNhatSoLuongSanPham(th)
                IDHangHoa = Integer.Parse(dt4.Rows(0)("ID"))
                th2.m_intIDDonNhapHang = IDNhapHang
                th2.m_intIDSanPham = IDHangHoa
                th2.m_intSoLuong = Integer.Parse(dgvDanhSach.Rows(i).Cells(3).Value)
                th2.m_intTongTien = Integer.Parse(dgvDanhSach.Rows(i).Cells(1).Value)
                TapHoaDAO.ChiTietNhapHang(th2)
            Else
                Dim th1 As TapHoa = New TapHoa()
                th1.m_strTenHangHoa = dgvDanhSach.Rows(i).Cells(0).Value
                th1.m_intSoLuong = Integer.Parse(dgvDanhSach.Rows(i).Cells(3).Value)
                th1.m_intGia = Integer.Parse(dgvDanhSach.Rows(i).Cells(1).Value) + ((Integer.Parse(dgvDanhSach.Rows(i).Cells(1).Value) * 10) / 100)
                th1.m_intIDDonVi = Integer.Parse(dgvDanhSach.Rows(i).Cells(2).Value)
                th1.m_intIDNhaCungCap = Integer.Parse(dgvDanhSach.Rows(i).Cells(4).Value)
                th1.m_intIDXuatXu = Integer.Parse(dgvDanhSach.Rows(i).Cells(5).Value)
                th1.m_intIDLoaiHang = Integer.Parse(dgvDanhSach.Rows(i).Cells(6).Value)
                th1.m_intIDViTri = Integer.Parse(dgvDanhSach.Rows(i).Cells(7).Value)
                th1.m_strHinhAnh = dgvDanhSach.Rows(i).Cells(8).Value
                Dim dt1 As DataTable = New DataTable()
                dt1 = TapHoaDAO.ThemHangHoa(th1)
                IDHangHoa = Integer.Parse(dt1.Rows(0)("ID"))
                th2.m_intIDDonNhapHang = IDNhapHang
                th2.m_intIDSanPham = IDHangHoa
                th2.m_intSoLuong = Integer.Parse(dgvDanhSach.Rows(i).Cells(3).Value)
                th2.m_intTongTien = Integer.Parse(dgvDanhSach.Rows(i).Cells(1).Value) * Integer.Parse(dgvDanhSach.Rows(i).Cells(3).Value)
                TapHoaDAO.ChiTietNhapHang(th2)
            End If

            i = i + 1
        End While
        Dim frm As frmNhapHangThanhCong = New frmNhapHangThanhCong()
        frm.Tag = IDNhapHang
        frm.Show()
    End Sub
End Class